#include "dgt_BeginEndControl.h"
#include <wx/image.h>
#include <wx/dc.h>
#include <wx/pen.h>


BEGIN_EVENT_TABLE (dgt_BeginEndControl, wxControl)
EVT_LEFT_DOWN (dgt_BeginEndControl::OnLeftDown)
EVT_MOTION (dgt_BeginEndControl::OnMove)
EVT_LEFT_UP (dgt_BeginEndControl::OnLeftUp)
EVT_PAINT (dgt_BeginEndControl::OnPaint)
EVT_LEFT_DCLICK (dgt_BeginEndControl::OnDClick)
EVT_CONTEXT_MENU (dgt_BeginEndControl::OnContextMenu)
EVT_MENU (wxID_DELETE, dgt_BeginEndControl::OnDeleteClick)
EVT_ERASE_BACKGROUND(dgt_BeginEndControl::OnEraseBackground)
EVT_ENTER_WINDOW(dgt_BeginEndControl::OnEnter)
EVT_LEAVE_WINDOW(dgt_BeginEndControl::OnLeave)

END_EVENT_TABLE ()

dgt_BeginEndControl::dgt_BeginEndControl(void)
{
}

dgt_BeginEndControl::~dgt_BeginEndControl(void)
{
}

dgt_BeginEndControl::dgt_BeginEndControl(wxWindow* parent, wxWindowID id, 
									 const wxPoint& pos, const wxSize& size, long style,
									 const wxValidator& validator
									 )
									 : wxControl(parent, id, pos, size, style , validator)
									 ,m_dragF( false ), m_drag(false), m_ldown(false), m_enter(false), m_top_marker(false),
									 m_bottom_marker(false)


{
}	
//Painting control
void dgt_BeginEndControl::OnPaint(wxPaintEvent& event)
{

	Refresh();
	wxPaintDC dc(this);
	
		if (IsEnabled())
		{
			if (m_drag||m_enter)
			{
				
				dc.SetPen (*wxWHITE_PEN);
				dc.SetBrush(*wxWHITE_BRUSH);
				dc.DrawRectangle( 0, 0, GetSize().GetX(), GetSize().GetY() );
				wxPen *pen1 = new wxPen (*wxBLACK, 1, wxDOT);
				dc.SetPen(*pen1);
				//dc.SetBrush( wxColour( 213, 212, 199 ) );
				dc.DrawRoundedRectangle( 0, 0, GetSize().GetX(), GetSize().GetY(), 30 );

			} 
			else
			{
			
			dc.SetBrush(*wxTRANSPARENT_BRUSH);
			dc.SetPen (*wxWHITE_PEN);
			dc.DrawRectangle( 0, 0, GetSize().GetX(), GetSize().GetY() );
			dc.SetBrush( *wxTRANSPARENT_BRUSH);
			wxPen *pen = new wxPen (wxColour(0,0,0), 1);
			dc.SetPen(*pen);
			dc.DrawRoundedRectangle( 0, 0, GetSize().GetX(), GetSize().GetY(), 30 );

			}
			/*if (m_top_marker) 
			{
				dc.SetPen (*wxWHITE_PEN);
				dc.DrawRectangle( 0, 0, 140,100 );
				wxPen *pen = new wxPen (*wxBLACK, 3);

				dc.SetPen(*pen);
				dc.SetBrush(*wxWHITE_BRUSH);
				dc.DrawRoundedRectangle( 0, 0, 140, 100, 30 );
				dc.SetBrush(*wxBLACK_BRUSH);
			//	dc.DrawRectangle(45,90, 10,10);
			}
			if (m_bottom_marker)
			{
				dc.SetPen (*wxWHITE_PEN);
				dc.DrawRectangle( 0, 0, 140,100 );
				wxPen *pen = new wxPen (*wxBLACK, 3);
				dc.SetPen(*pen);
				dc.SetBrush(*wxWHITE_BRUSH);
				dc.DrawRoundedRectangle( 0, 0, 140, 100, 30 );
				dc.SetBrush(*wxBLACK_BRUSH);
				//dc.DrawRectangle(45,0, 10,10);
			}*/
			/*dc.DrawRectangle(75,140, 10,10);
			dc.DrawRectangle(75,0, 10,10);*/
		}

}
/***********************************************************************************************/
wxSize dgt_BeginEndControl::DoGetBestSize() const
{

	wxSize best;


	best.x = 100;
	best.y = 100;


	return best;


}

/*****************************************************************/
//Adding text to the control
void dgt_BeginEndControl::AddText(wxString  string)
{

	wxStaticText * statText;
	this->Refresh(true,NULL);
	statText = new wxStaticText(this, wxID_ANY, string, wxPoint(30,30),
		wxDefaultSize, wxALIGN_RIGHT , wxEmptyString);

}

/************************************************************************/
//On mouse double click event appears adding text dialog
void dgt_BeginEndControl::OnDClick(wxMouseEvent& event)
{
	this->DestroyChildren();
	wxTextEntryDialog * TextDlg = new wxTextEntryDialog(this, _T("Please, enter text"), _T("Text dialog"),
		_T(""), wxOK | wxCANCEL | wxCENTRE, wxDefaultPosition);
	TextDlg->ShowModal();
	wxString string;
	string = TextDlg->GetValue();
	AddText(string);
}
/*******************************************************/
//On mouse left button down event
void dgt_BeginEndControl::OnLeftDown(wxMouseEvent &event )
{
	Refresh();
	CaptureMouse(); 
	m_ldown = true;
	wxPoint pos = ClientToScreen(event.GetPosition()); 
	wxPoint origin = GetPosition(); 
	int dx =  pos.x - origin.x; 
	int dy = pos.y - origin.y;
	m_Point = wxPoint(dx, dy);
}
/****************************************/
//It's a kind of moving control
void dgt_BeginEndControl::OnMove(wxMouseEvent &event)
{
	Refresh();
	wxPoint pt = event.GetPosition(); 
	if (event.Dragging() && event.LeftIsDown()) 
	{ 
			m_drag = true;
		wxPoint pos = ClientToScreen(pt); 
		Move(wxPoint(pos.x - m_Point.x, pos.y - m_Point.y)); 
	}
	if ((pt.x>=50)&&(pt.x<=83)&&(pt.y>=40)&&(pt.y<=100)) m_top_marker = true;
	else if ((pt.x>=50)&&(pt.x<=83)&&(pt.y<=20)) m_bottom_marker = true;
	else { m_bottom_marker = false; m_top_marker = false;}

}

/***************************************************/
////On mouse left button up event
void dgt_BeginEndControl::OnLeftUp(wxMouseEvent & event)
{
	if (HasCapture()) 
	{ 
		ReleaseMouse(); 
	}
	m_drag = false;
	m_ldown = false;

}
/************************************************************/
//appending context menu when right button is pressed on the control
void dgt_BeginEndControl::OnContextMenu(wxContextMenuEvent & event)
{

	wxPoint point = event.GetPosition();
	if (point.x == -1 && point.y == -1) {
		wxSize size = GetSize();
		point.x = size.x / 2;
		point.y = size.y / 2;
	} 
	else 
	{
		point = ScreenToClient(point);
	}
	wxMenu menu;

	menu.Append(wxID_DELETE, _T("&Delete"));
	menu.AppendSeparator();
	menu.Append(wxID_CANCEL, _T("Cancel"));

	this->PopupMenu(&menu, point.x, point.y);

}
/***********************************************************/
//deleting control
void dgt_BeginEndControl::OnDeleteClick(wxCommandEvent &event)
{
	this->Destroy();
}
/****************************************************************/
//I suppose it avoids flickering but it doesn't
void dgt_BeginEndControl::OnEraseBackground(wxEraseEvent &WXUNUSED(event)) 
{ 
	//don't do anything here 
} 
/*********************************************************/
//Checks if mouse enters control and changes flag if it does
void dgt_BeginEndControl::OnEnter( wxMouseEvent &event )
{

	m_enter = true;
	Refresh();
}

/**********************************************************************************************/
//Checks if mouse leaves control and changes flag if it does
void dgt_BeginEndControl::OnLeave( wxMouseEvent &event )
{
	m_enter = false;
	Refresh();
}
/*******************************************************************/

/**************************************************/
//checks if control is enable and changes flags if it isn't
void dgt_BeginEndControl::SetEnable( bool c_enable )
{
	m_enableF = c_enable;

	if( !m_enableF )
	{
		m_dragF = false;
		
	}

	Refresh();
}
/**************************************************/
